package com.wyedu.qqMessageTransfer;

import com.wyedu.qqServer.service.ManageServerConnectClientThread;
import com.wyedu.qqServer.service.QQServer;
import com.wyedu.qqcommon.Message;
import com.wyedu.qqcommon.MessageType;

import java.io.ObjectOutputStream;
import java.net.Socket;

public class qqMessageTransfer {


    private Socket socket;

    public qqMessageTransfer(Socket socket) {
        this.socket = socket;
    }

    //负责消息传输的类
    //1.普通消息

    public void privateMessage(Message message){
        //1.源用户
        //2.目的用户
        String requestId = message.getSender();
        String responseId = message.getGetter();

        //3.要发送的消息/服务器返回的消息
        try {
            ObjectOutputStream responseOOS = new ObjectOutputStream(ManageServerConnectClientThread.getServerConnectClientThread(responseId).getSocket().getOutputStream());

            //4.创建两个message对象
          //  Message requestMessage = new Message();
            Message responseMessage = new Message();
            //5.服务器返回发送成功/服务器给目标用户发送私聊内容
            //6.消息类型
          //  requestMessage.setMesType(MessageType.MESSAGE_COMM_MES);
            responseMessage.setMesType(MessageType.MESSAGE_COMM_MES);
            //7.消息内容
           // requestMessage.setContent("发送成功");
            responseMessage.setContent(message.getContent());
            //8.发送用户
          //  requestMessage.setSender("服务器");
            responseMessage.setSender(requestId);
            //9.接收用户,不用写了
            //10.发送
          //  requestOOS.writeObject(requestMessage);
            responseOOS.writeObject(responseMessage);
            feedback("消息发送成功");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //2.群聊消息
    public void groupChat(Message message){
        System.out.println(message.getSender()+"对所有人说" + message.getContent());
        String[]users = ManageServerConnectClientThread.getOnLineUsers().split(" ");
        for (String id:users) {
            if(id.equals(" ")){
                //为这个说明最后一个遍历到了
                break;
            }
            try {
                //获取每一个目标用户的socket
                Socket socket1 = ManageServerConnectClientThread.getServerConnectClientThread(id).getSocket();
              //如果不是发起该群聊的用户
                if (!id.equals(message.getSender())) {
                    Message responseMessage = new Message();
                    //发送消息的用户
                    responseMessage.setSender(message.getSender());
                    //发送消息的类型
                    responseMessage.setMesType(MessageType.MESSAGE_COMM_MES);
                    //群聊的信息
                    responseMessage.setContent(message.getContent());

                    new ObjectOutputStream(socket1.getOutputStream()).writeObject(responseMessage);
                } else {
                   feedback("群聊消息发送成功");
                }
            }catch (Exception e){
                  e.printStackTrace();
              }

            }

    }

    //3.文件
    public void sendFile(Message message){
        System.out.println("用户("+message.getSender()+")要发送文件");
        Message responseMessage = new Message();
        //发送用户
        responseMessage.setSender(message.getSender());
        //接收用户
        responseMessage.setGetter(message.getGetter());
        //文件名
        responseMessage.setContent(message.getContent());
        //文件内容
        responseMessage.setFileMessage(message.getFileMessage());
        //消息类型
        responseMessage.setMesType(MessageType.MESSAGE_COMM_FILE);

        try {
            new ObjectOutputStream(ManageServerConnectClientThread.
                    getServerConnectClientThread(message.getGetter()).getSocket().getOutputStream()).writeObject(responseMessage);
        }catch (Exception e){
            e.printStackTrace();
        }
        feedback("文件发送成功");

    }

    //4.群发文件
    public void GroupFile(Message message){
        System.out.println("用户("+message.getSender()+")要发送群文件");
        String[]users = ManageServerConnectClientThread.getOnLineUsers().split(" ");
        for (String id:users) {
            if(id.equals(" ")){
                //为这个说明最后一个遍历到了
                break;
            }
            try {
                //获取每一个目标用户的socket
                Socket socket1 = ManageServerConnectClientThread.getServerConnectClientThread(id).getSocket();
                //如果不是发起该群聊的用户
                if (!id.equals(message.getSender())) {
                    Message responseMessage = new Message();
                    //发送消息的用户
                    responseMessage.setSender(message.getSender());
                    //发送消息的类型
                    responseMessage.setMesType(MessageType.MESSAGE_COMM_FILE);
                    //发送的文件名
                    responseMessage.setContent(message.getContent());
                    //接收消息的用户
                    responseMessage.setGetter(id);
                    //结束的文件
                    responseMessage.setFileMessage(responseMessage.getFileMessage());
                    new ObjectOutputStream(socket1.getOutputStream()).writeObject(responseMessage);
                } else {
                    feedback("群文件成功");
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }

    }
    //5.请求在线用户列表
    public void returnAllOnlineUsers(Message message){
        System.out.println("用户("+message.getSender()+")要在线用户列表");
        Message responseMessage = new Message();
        //1.消息类型
        responseMessage.setMesType(MessageType.MESSAGE_RET_ONLINE_FRIEND);
        //2.把这在线用户列表放入
        responseMessage.setContent(ManageServerConnectClientThread.getOnLineUsers());
        try {
           ObjectOutputStream requestOOS = new ObjectOutputStream(socket.getOutputStream());
            requestOOS.writeObject(responseMessage);
            requestOOS.flush();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void UserDoesNotExist(Message message) {
        Socket socket = ManageServerConnectClientThread.getServerConnectClientThread(message.getSender()).getSocket();
        feedback("该用户不存在");
    }
    //5.关闭该连接
    public void exit(Message message) {

       Message responseMessage = new Message();
       try {
           responseMessage.setMesType(MessageType.MESSAGE_CLIENT_EXIT);
           ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
           oos.writeObject(responseMessage);//需要先将消息传回去再断开连接
       }catch (Exception e){
           e.printStackTrace();
       }


    }
    //6.返回给发送方的信息
    public void feedback(String str){
        try {
           Message requestMessage = new Message();
           requestMessage.setContent(str);
           requestMessage.setSender("服务器");
           requestMessage.setMesType(MessageType.MESSAGE_COMM_MES);
           new ObjectOutputStream(socket.getOutputStream()).writeObject(requestMessage);

        }catch (Exception e){
            e.printStackTrace();
        }

    }



}
